
import time
import timeit

class TimerManager(object):
    ''' Manages a set of timers '''
    def __init__(self):
        self.name_to_timer = dict()
    def create_timer(self,timer_name):
        self.name_to_timer[timer_name] = Timer()
    
    def start(self,timer_name):
        self.name_to_timer[timer_name].start()
    def stop(self,timer_name):
        self.name_to_timer[timer_name].stop()
    def get_times(self,timer_name):
        return self.name_to_timer[timer_name].times
    def get_total_time(self,timer_name):
        if self.name_to_timer[timer_name].started == True:
            self.name_to_timer[timer_name].stop()
        return self.name_to_timer[timer_name].sum()
     
class Timer(object):
    def __init__(self):
        self.started = False
        self.start_time = 0
        self.times = []
    def start(self):
        self.start_time = time.clock()
        self.started = True
    def stop(self):
        end_time = time.clock()
        self.times.append(end_time-self.start_time)
        self.started=False
    def sum(self):
        total_time = 0        
        for time in self.times:
            total_time = total_time+time
        if self.started: # Started but not stopped
            total_time =total_time - self.times[-1]
        return total_time
    
timer_manager = TimerManager()